﻿using PhotoKiosk.DataAccess;
using PhotoKiosk.Properties;
using PhotoKiosk.ViewModel.Helpers;
using System.Resources;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            SystemData = new DataAccess.SystemData();
            System.Threading.Thread.CurrentThread.CurrentUICulture = SystemData.CurrentLanguage;
            CurrentViewModel = new SelectSourceViewModel(this);
        }

        private ResourceManager m_resourceManager = null;
        public ResourceManager ResourceManager
        {
            get
            {
                if (m_resourceManager == null)
                {
                    m_resourceManager = new ResourceManager(typeof(Resources));
                }
                return m_resourceManager;
            }
        }

        private ViewModelBase m_currentViewModel;
        public ViewModelBase CurrentViewModel
        {
            get
            {
                if (m_currentViewModel == null)
                {
                    m_currentViewModel = new SelectSourceViewModel(this);
                }
                return m_currentViewModel;
            }
            set
            {
                m_currentViewModel = value;
                OnPropertyChanged("CurrentViewModel");
            }
        }

        public SystemData SystemData
        {
            get;
            private set;
        }

        #region Exit Command
        private DelegateCommand m_exitCommand;
        public ICommand ExitCommand
        {
            get
            {
                if (m_exitCommand == null)
                {
                    m_exitCommand = new DelegateCommand(param => this.exitCommandExecute(), param => this.exitCommandCanExecute());
                }
                return m_exitCommand;
            }
        }

        private bool exitCommandCanExecute()
        {
            return !CurrentViewModel.GetType().Equals(typeof(ExitSystemViewModel));
        }

        private void exitCommandExecute()
        {
            ExitSystemViewModel esvm = new ExitSystemViewModel(this, CurrentViewModel);
            CurrentViewModel = esvm;
        }
        #endregion

        #region Cancel Order Command
        private DelegateCommand m_cancelOrderCommand;
        public ICommand CancelOrderCommand
        {
            get
            {
                if (m_cancelOrderCommand == null)
                {
                    m_cancelOrderCommand = new DelegateCommand(param => this.cancelOrderCommandExecute(), param => this.cancelOrderCommandCanExecute());
                }
                return m_cancelOrderCommand;
            }
        }

        private bool cancelOrderCommandCanExecute()
        {
            return !(CurrentViewModel.GetType().Equals(typeof(SelectSourceViewModel)) || CurrentViewModel.GetType().Equals(typeof(ExitSystemViewModel)) || CurrentViewModel.GetType().Equals(typeof(SystemSettingsViewModel)));
        }

        private void cancelOrderCommandExecute()
        {
            CurrentViewModel = new SelectSourceViewModel(this);
        }
        #endregion
    }
}
